package com.lute.model;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;

import com.lute.utils.DAOFactory;

@Entity
public class ClientProfile {
	private int id_client_profile;
	//private String clientName; check if it will be neccessary for others profiles -> useful if not 1:1 but 1 to many
	private Client client;
	private GregorianCalendar deadlineToCloseAccountingPeriod;
	private GregorianCalendar sendMailRemainder;
	private List<ClientAdmin> clientAdmins = new ArrayList<ClientAdmin>();
	private List<ClientCategory> clientCategories = new ArrayList<ClientCategory>();
		
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)	
	public int getId_client_profile() {
		return id_client_profile;
	}
	public void setId_client_profile(int id_client_profile) {
		this.id_client_profile = id_client_profile;
	}
	@OneToOne(cascade=CascadeType.ALL)
	@JoinColumn(name="id_client")
	public Client getClient() {
		return client;
	}
	public void setClient(Client client) {
		this.client = client;
	}
	public GregorianCalendar getDeadlineToCloseAccountingPeriod() {
		return deadlineToCloseAccountingPeriod;
	}
	public void setDeadlineToCloseAccountingPeriod(
			GregorianCalendar deadlineToCloseAccountingPeriod) {
		this.deadlineToCloseAccountingPeriod = deadlineToCloseAccountingPeriod;
	}
	public GregorianCalendar getSendMailRemainder() {
		return sendMailRemainder;
	}
	public void setSendMailRemainder(GregorianCalendar sendMailRemainder) {
		this.sendMailRemainder = sendMailRemainder;
	}

	@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
	public List<ClientAdmin> getClientAdmin() {
		return clientAdmins;
	}
	public void setClientAdmin(List<ClientAdmin> clientAdmin) {
		this.clientAdmins = clientAdmin;
	}
	@OneToMany(targetEntity=ClientCategory.class,cascade=CascadeType.ALL,fetch=FetchType.LAZY, mappedBy="clientProf")
	public List<ClientCategory> getClientCategories() {
		return clientCategories;
	}
	public void setClientCategories(List<ClientCategory> clientCategories) {
		this.clientCategories = clientCategories;
	}
	
	public static List<String> getClientsAdminEmail(String propertyValue, int propertyName) {
		ClientProfile clientProfile = getClientProfileWithClientAdminsFromDB(propertyValue,propertyName);
		List<ClientAdmin> clientAdmins = clientProfile.getClientAdmin();
		List<String> emails = new ArrayList<String>();
		for(ClientAdmin clientAdmin : clientAdmins) {
			emails.add(clientAdmin.getEmail());
		}
		return emails;
	}
	
	public static ClientProfile getClientProfileWithClientAdminsFromDB(String propertyName, int propertyValue) {
		return DAOFactory.getInstance().getLuteInterface().getClientProfileByIdWithClientsAdmin(propertyName, propertyValue);
	}
	
	public static List<ClientProfile> getClientProfileWithClientAdminsFromDB(String propertyName, int[] propertyValue) {
		return DAOFactory.getInstance().getLuteInterface().getClientProfileByIdWithClientsAdmin(propertyName, propertyValue);
	}
	
	public static void addClientProfileToDB(ClientProfile clientprofile) {
		DAOFactory.getInstance().getLuteInterface().addObjectToDataBase(clientprofile);
	}
	
	public static void updateClientProfileInDB(ClientProfile clientprofile) {
		DAOFactory.getInstance().getLuteInterface().updateObjectinDataBase(clientprofile);
	}
}
